package com.aijia.custom.product.ui;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.aijia.custom.product.R;
import com.aijia.custom.product.customview.LoadingWaitDialog;
import com.aijia.custom.product.customview.NoDoubleClickListener;
import com.aijia.custom.product.listener.IModifyPwdListener;
import com.aijia.custom.product.manager.ModifyPwdManager;
import com.aijia.custom.product.utils.Md5Utils;
import com.aijia.custom.product.utils.Notification;
import com.aijia.custom.product.utils.SharedPreferencesUtils;

/**
 * Created by yejianjun on 2019/2/19.
 */

public class ModifyPasswordActivity extends Activity implements IModifyPwdListener {

    private ImageButton backBtn;
    private EditText initPassword, nPassword, confirmPassword;
    private Button submitBtn;
    private ModifyPwdManager manager;
    private Dialog pDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_password);
        initView();
        initManager();
    }

    private void initManager() {
        manager = new ModifyPwdManager(this);
        manager.addModifyPwdListener(this);
    }

    private void initView() {
        backBtn = findViewById(R.id.back_button);
        initPassword = findViewById(R.id.original_password);
        nPassword = findViewById(R.id.new_password);
        confirmPassword = findViewById(R.id.confirm_password);
        submitBtn = findViewById(R.id.submit_btn);

        submitBtn.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (check()) {
                    showDialog();
                    reqData();
                }
            }
        });

        backBtn.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
    }

    private void reqData() {
        manager.modifyPwd(Md5Utils.Md5(initPassword.getText().toString().trim()), Md5Utils.Md5(nPassword.getText().toString().trim()));
    }


    private boolean check() {
        boolean ret = false;
        if (TextUtils.isEmpty(initPassword.getText().toString().trim())) {
            Notification.toast(this, "原密码不能为空");
            initPassword.requestFocus();
            return ret;
        } else if (TextUtils.isEmpty(nPassword.getText().toString().trim())) {
            Notification.toast(this, "原密码不能为空");
            nPassword.requestFocus();
            return ret;
        } else if (TextUtils.isEmpty(confirmPassword.getText().toString().trim())) {
            Notification.toast(this, "确认密码不能为空");
            confirmPassword.requestFocus();
            return ret;
        }

        if (!confirmPassword.getText().toString().trim().equals(nPassword.getText().toString().trim())) {
            Notification.toast(this, "确认密码与新密码不一致");
            confirmPassword.requestFocus();
            return ret;
        }

        return true;
    }

    private void showDialog() {
        pDialog = LoadingWaitDialog.createLoadingDialog(this, "等待中...");
        pDialog.show();

    }

    @Override
    public void onModifyPwdFailure(int errorCode, String errorMsg) {
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
        Notification.toast(this, errorMsg == null ? "" : errorMsg);
    }

    @Override
    public void onModifyPwdSuccess() {
        if (null != pDialog && pDialog.isShowing()) {
            pDialog.dismiss();
        }
        Notification.toast(this, "新密码修改成功");
        // 保存新密码
        SharedPreferencesUtils.setPassword(this, Md5Utils.Md5(nPassword.getText().toString().trim()));
    }
}
